package fr.esiag.isidis.pdsTeam.Observer;

import java.awt.BorderLayout;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;


import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import fr.esiag.isidis.pdsTeam.Enums.Source;
import fr.esiag.isidis.pdsTeam.TramCreator.Tram;
import fr.esiag.isidis.pdsTeam.UDP.RawInformationSender;
import fr.esiag.isidis.pdsTeam.beans.EtatTrain;
import fr.esiag.isidis.pdsTeam.views.TrainView;

public class TableObserver extends JPanel implements Observer{
	ButtonEditor buttonEditor;
	ButtonRenderer buttonRenderer;
	Map<Integer, TrainView> map;
	Object []columns =new Object[] {"Train","Station","destination","etat","Position","Button","Démarrer/Arreter"};
	private DefaultTableModel dm;
	private JTable table;
	boolean terminus;
  public TableObserver(Map<Integer, TrainView> map){
    super();
    this.map=map;
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        TrainView train=((TrainView)pairs.getValue());
			train.getEtat().addObserver(this);
    }
    this.setLayout(new BorderLayout());
    dm = new DefaultTableModel();
    dm.setDataVector(getRows(), columns);
    table = new JTable(dm);
    buttonRenderer=new ButtonRenderer();
    buttonEditor=new ButtonEditor(new JCheckBox(),map);
    table.getColumn("Button").setCellRenderer(buttonRenderer);
    table.getColumn("Button").setCellEditor(buttonEditor);
    table.getColumn("Démarrer/Arreter").setCellRenderer(buttonRenderer);
    table.getColumn("Démarrer/Arreter").setCellEditor(buttonEditor);
    JScrollPane scroll = new JScrollPane(table);
    add(scroll,BorderLayout.CENTER);
    //setSize(400, 100);
    setVisible(true);
    
  }
  		
		
  		Object[][] getRows(){
  			Object [][]s=new Object[map.size()][columns.length];
  			 Iterator it = map.entrySet().iterator();
  			 int i=0;
  			    while (it.hasNext()) {
  			        Map.Entry pairs = (Map.Entry)it.next();
  			        TrainView train=((TrainView)pairs.getValue());
  						s[i]=train.getRow(columns.length);
  						s[i][6]=(train.isEnMarche())?"Arreter":"Démarrer";
  						i++;
  			    }
  			return s;
  		}


		public void update(Observable arg0, Object arg1) {
			// TODO Auto-generated method stub
			EtatTrain etatTrain=(EtatTrain)arg0;
				//System.out.println(Source.Train.ordinal()+","+etatTrain.getIdT()+","+etatTrain.getStation());
				byte[] stationA=Tram.getInstance().setTram(Source.Train.ordinal(),etatTrain.getIdT(),etatTrain.getStation());
					try {
						RawInformationSender.getInstance().sendTram(stationA);
					} catch (UnknownHostException e) {
						e.printStackTrace();
					}
				dm.setDataVector(getRows(),columns);
				table.getColumn("Button").setCellRenderer(buttonRenderer);
			    table.getColumn("Button").setCellEditor(buttonEditor);
			    table.getColumn("Démarrer/Arreter").setCellRenderer(buttonRenderer);
			    table.getColumn("Démarrer/Arreter").setCellEditor(buttonEditor);
		}		
}




